
import {baseUrl} from './request.js'; 
// 格式化当前时间
export function parseDate() {
	var nowDate = new Date();
	var year = nowDate.getFullYear();
	var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
	var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
	return {'year':year,'month':month,'day':day};
}

// ArrayBuffer转16进度字符串示例
export function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function(bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
}

// 获取网址参数
export function getUrlJson(url) {
	    url = decodeURIComponent(url);
		let index = url.indexOf("?");
		let result = url.substr(index + 1,url.length);
        result = result.split("&");
		let arr = [];
		let data = {};
		for(let i = 0 ; i< result.length;i++){
			let arrs = result[i].split("=");
			data[arrs[0]] = arrs[1];
		}		
		return data;
    }

//  下载图片
export function downloadImages(imageurl){
	imageurl = imageurl.replace("/api/v2/","")
	uni.showLoading({
		title: '加载中'
	});
	return new Promise((resolve, reject) => {
		uni.downloadFile({
			url: baseUrl + imageurl,
			header:{"Authorization":uni.getStorageSync('token')},
		}).then((response) => {
			uni.hideLoading();
			let [error, res] = response;
			resolve(res);
		})	
	})
}
// 上传单张图片
export function uploadImage(imageurl,url = 'file-service/upload/tenant/news'){
	uni.showLoading({
		title: '加载中'
	});
	return new Promise((resolve, reject) => {
	uni.uploadFile({
		url: baseUrl + url,
		name: 'file',
		filePath:imageurl,
		formData: {
			file:imageurl,
		},
		header:{"Authorization":uni.getStorageSync('token'),"Content-Type": "multipart/form-data"},
	}).then((response) => {
			uni.hideLoading();
			let [error, res] = response;
			resolve(res.data);
		})	
	})
}
